package com.wuliqing.android.zte.view;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wuliqing.android.zte.data.ContactData;
import com.wuliqing.android.zte.drawable.RoundImageDrawable;
import com.wuliqing.android.zte.myapplication.R;

//import butterknife.ButterKnife;
//import butterknife.InjectView;

/**
 * Author:    ZhuWenWu
 * Version    V1.0
 * Date:      2015/2/25  18:21.
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/2/25        ZhuWenWu            1.0                    1.0
 * Why & What is modified:
 */
public class RecyclerViewsAdapter extends BaseAbstractRecycleCursorAdapter<RecyclerView.ViewHolder> {
    private Context mContext;
    public RecyclerViewsAdapter(Context context) {
        super(context, null);
        mContext = context;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, Cursor cursor) {
        ContactData contactData = ContactData.fromCursor(cursor,mContext);
        final View view = ((ViewHolder)holder).mView;
        ((ViewHolder)holder).name_v.setText(contactData.getContact_name());
        if (contactData.getContact_photo_bitmap() != null) {
            ((ViewHolder)holder).photo_v.setImageDrawable(new RoundImageDrawable(contactData.getContact_photo_bitmap()));
        } else {
            Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.cheese_3);
            ((ViewHolder)holder).photo_v.setImageDrawable(new RoundImageDrawable(bitmap));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_card_main, parent, false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public final View mView;
        public TextView name_v;
        public ImageView photo_v;
        public ViewHolder(View view) {
            super(view);
            mView = view;
            mView.setOnClickListener(this);
            mView.setOnLongClickListener(this);
            name_v = (TextView)view.findViewById(R.id.contact_name);
            photo_v = (ImageView)view.findViewById(R.id.contact_photo_img);
            photo_v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onRecyclerViewListener){
                        Cursor cursor = (Cursor)getItem(getAdapterPosition());
                        ContactData mContactData = ContactData.fromCursor(cursor, mContext);
                        onRecyclerViewListener.onSelectImageClick(mContactData);
                    }
                }
            });
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                Cursor cursor = (Cursor)getItem(getAdapterPosition());
                ContactData mContactData = ContactData.fromCursor(cursor, mContext);
                onRecyclerViewListener.onItemClick(mContactData);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                Cursor cursor = (Cursor)getItem(getAdapterPosition());
                ContactData mContactData = ContactData.fromCursor(cursor, mContext);
                return onRecyclerViewListener.onItemLongClick(mContactData);
            }
            return false;
        }
    }

    public interface OnRecyclerViewListener {
        void onItemClick(ContactData mContactData);

        boolean onItemLongClick(ContactData mContactData);

        void onCallButtonClick(ContactData mContactData);

        void onSelectImageClick(ContactData mContactData);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }
}
